package com.kun.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.domain.SysUser;
import com.kun.service.mapper.SysUserMapper;
import com.kun.service.service.ISysUserService;
import com.kun.utils.BeanUtils;
import com.kun.vo.SysUserVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    /**
     * 通过用户名查询用户
     */
    @Override
    public SysUser selectUserByUserName(String userName) {
        return lambdaQuery().eq(SysUser::getName, userName).eq(SysUser::getDelFlag, "0")
                .last("LIMIT 1").one();
    }

    /**
     * 根据条件分页查询用户列表
     */
    @Override
    public List<SysUser> selectUserList(SysUser sysUser) {
        return baseMapper.selectUserList(sysUser);
    }

    /**
     * 添加信息
     */
    @Override
    public List<SysUserVo> addInfo(List<SysUser> sysUsers) {
        Map<Long, SysUser> collect = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
        List<SysUserVo> sysUserVos = BeanUtils.copyList(SysUserVo.class, sysUsers);
        sysUserVos.forEach(sysUserVo -> sysUserVo.setDeptName(collect.get(sysUserVo.getUserId()).getDept().getDeptName()));
        return sysUserVos;
    }
}
